package com.fyl.verify.aop;

import com.fyl.verify.anno.AuthVerify;
import com.fyl.verify.anno.ParamVerify;
import com.fyl.verify.anno.Verify;
import org.aopalliance.aop.Advice;
import org.springframework.aop.Pointcut;
import org.springframework.aop.support.AbstractPointcutAdvisor;
import org.springframework.aop.support.ComposablePointcut;
import org.springframework.aop.support.annotation.AnnotationMatchingPointcut;

public class VerifyAnnotationAdvisor extends AbstractPointcutAdvisor {

    private Pointcut pointcut;

    private Advice advice;

    public VerifyAnnotationAdvisor(Advice advice) {
        this.advice = advice;
        this.pointcut = buildPointcut();
    }

    @Override
    public Pointcut getPointcut() {
        return pointcut;
    }

    @Override
    public Advice getAdvice() {
        return advice;
    }

    private Pointcut buildPointcut() {
        Pointcut cpc = AnnotationMatchingPointcut.forClassAnnotation(Verify.class);
        Pointcut mpc = AnnotationMatchingPointcut.forMethodAnnotation(Verify.class);
        Pointcut mpc2 = AnnotationMatchingPointcut.forMethodAnnotation(AuthVerify.class);
        Pointcut mpc3 = AnnotationMatchingPointcut.forMethodAnnotation(ParamVerify.class);
        return new ComposablePointcut(cpc).union(mpc).union(mpc2).union(mpc3);
    }
}
